package com.rc.controller;


import com.rc.annotation.Authorization;

import com.rc.annotation.CurrentUser;
import com.rc.mode.User;
import com.rc.service.SayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.condition.PatternsRequestCondition;
import org.springframework.web.servlet.mvc.condition.RequestMethodsRequestCondition;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Date: 2018/10/19 14:58
 */
@RestController
public class demoController {
    @Autowired
    private SayService sayService;

    @Autowired
    WebApplicationContext applicationContext;


    @Authorization
    @RequestMapping("say2")
    public String say(HttpServletRequest request, @CurrentUser User user)  {


        System.out.println(request.getHeader("CURRENT_USER"));
        return sayService.say();
    }


    @RequestMapping(value = "v1/getAllUrl")
    public Object getAllUrl() {
        RequestMappingHandlerMapping mapping = applicationContext.getBean(RequestMappingHandlerMapping.class);
        // 获取url与类和方法的对应信息a
        Map<RequestMappingInfo, HandlerMethod> map = mapping.getHandlerMethods();

//		List<String> urlList = new ArrayList<>();
//		for (RequestMappingInfo info : map.keySet()) {
//			// 获取url的Set集合，一个方法可能对应多个url
//			Set<String> patterns = info.getPatternsCondition().getPatterns();
//
//			for (String url : patterns) {
//				urlList.add(url);
//			}
//		}

        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        for (Map.Entry<RequestMappingInfo, HandlerMethod> m : map.entrySet()) {




            Map<String, Object> map1 = new HashMap<String, Object>();
            RequestMappingInfo info = m.getKey();
            HandlerMethod method = m.getValue();


                map1.put("authorization", 0);
            if ( method.getMethod().getAnnotation(Authorization.class) != null) {
                map1.put("authorization", 1);
            }

            PatternsRequestCondition p = info.getPatternsCondition();
            for (String url : p.getPatterns()) {
                map1.put("url", url);
            }
            map1.put("className", method.getMethod().getDeclaringClass().getName()); // 类名
            map1.put("method", method.getMethod().getName()); // 方法名
            map1.put("classMethod",  method.getMethod().getDeclaringClass().getName()+"."+method.getMethod().getName()); // 方法名
            map1.put("type", "GET");
            RequestMethodsRequestCondition methodsCondition = info.getMethodsCondition();

            for (RequestMethod requestMethod : methodsCondition.getMethods()) {
                map1.put("type", requestMethod.toString());
            }

            list.add(map1);
        }


        return list;
    }





}
